<?php
namespace Home\Model;
use Think\Model;
class BannerModel extends Model{
	private $DescriptionModel;
	function _initialize(){
		parent::_initialize();
		$this->DescriptionModel=M('BannerImageDescription');
	}
	
	public function getBanner($banner_name=''){
		$bst=$this->where(array('name'=>$banner_name))->find();
		$banner_id=$bst['banner_id'];
		$map['banner_id']=$banner_id;

		$banner=$this->where($map)->find();


		$ImageModel=M('BannerImage');
		
		$banner_image=$ImageModel->where($map)->select();
		foreach ($banner_image as $key=> $image) {
			$banner_image[$key]['banner_image_description']=$this->getBannerImageDescription($image['banner_image_id']);
		}
		$banner['banner_image']=$banner_image;
		return $banner;

	}

	private function getBannerImageDescription($banner_image_id){
		$return=array();
		$map['banner_image_id']=$banner_image_id;
		$map['language_id']=get_default_lng_id();
		$return=$this->DescriptionModel->where($map)->find();
		
		return $return;
	}

	public function del($banner_id){
		$ImageModel=M('BannerImage');
		$map['banner_id']=$banner_id;
		$banner_image=$ImageModel->where($map)->select();
		foreach ($banner_image as $v) {
			$this->DescriptionModel->where(array('banner_image_id'=>$v['banner_image_id']))->delete();
		}
		$ImageModel->where($map)->delete();
		return $this->where($map)->delete();
	}
}